# License Agreement (3-clause BSD License)
# Copyright (c) 2015, Klaus Haag, all rights reserved.
# Third party copyrights and patents are property of their respective owners.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the names of the copyright holders nor the names of the contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# This software is provided by the copyright holders and contributors "as is" and
# any express or implied warranties, including, but not limited to, the implied
# warranties of merchantability and fitness for a particular purpose are disclaimed.
# In no event shall copyright holders or contributors be liable for any direct,
# indirect, incidental, special, exemplary, or consequential damages
# (including, but not limited to, procurement of substitute goods or services;
# loss of use, data, or profits; or business interruption) however caused
# and on any theory of liability, whether in contract, strict liability,
# or tort (including negligence or otherwise) arising in any way out of
# the use of this software, even if advised of the possibility of such damage.

project(CfTracking)

cmake_minimum_required(VERSION 3.0)

# IF(NOT DEFINED CMAKE_BUILD_TYPE)
#   SET(CMAKE_BUILD_TYPE "Release")
# ENDIF()

# SET MODULE_OUT_PATH: Be aware of CMAKE_CURRENT_BINARY_DIR
IF( NOT DEFINED MODULE_OUT_PATH )
  SET( MODULE_OUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/lib/ )
ENDIF()
SET( LIBRARY_OUTPUT_PATH ${MODULE_OUT_PATH} )

SET( TARGET_BUILD_FLAGS
  -Wall
  -pthread
  "$<$<CONFIG:Release>:-O3>"
  "$<$<NOT:$<CONFIG:Release>>:-O0>"
  "$<$<NOT:$<CONFIG:Release>>:-ggdb>"
)

# Set the C++ options
SET(CMAKE_CXX_STANDARD 14)
SET(CXX_EXTENSIONS OFF)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

# option to enable OpenMP; only relevant for the KCF version with the
# VOT scale estimation
option(WITH_OPENMP "Enable OpenMP" OFF)

if(WITH_OPENMP)
    find_package(OpenMP REQUIRED)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(WITH_OPENMP)

# add OpenCV
find_package(OpenCV 4 REQUIRED)

# add 3rdparty source
set(CF_TCLAP_DIR "src/3rdparty/tclap")
set(CF_CV_EXT_DIR "src/3rdparty/cv_ext")
set(CF_PIOTR_DIR "src/3rdparty/piotr")

                    
set(CF_PIOTR_SOURCES
    ${CF_PIOTR_DIR}/gradientMex.hpp
    ${CF_PIOTR_DIR}/src/gradientMex.cpp
    ${CF_PIOTR_DIR}/src/sse.hpp
    ${CF_PIOTR_DIR}/src/wrappers.hpp)

set(CF_LIB_COMMON_SOURCES
    src/cf_libs/common/feature_channels.hpp
    src/cf_libs/common/mat_consts.hpp
    src/cf_libs/common/math_helper.hpp
    src/cf_libs/common/math_helper.cpp
    src/cf_libs/common/cf_tracker.hpp
    src/cf_libs/common/tracker_debug.hpp
    src/cf_libs/common/scale_estimator.hpp
    src/cf_libs/common/cn_feature.hpp
    src/cf_libs/common/cn_feature.cpp
    src/cf_libs/common/cv_ext.hpp
    ${CF_CV_EXT_DIR}/shift.cpp
    ${CF_CV_EXT_DIR}/shift.hpp
    ${CF_CV_EXT_DIR}/math_spectrums.cpp
    ${CF_CV_EXT_DIR}/math_spectrums.hpp
    ${CF_CV_EXT_DIR}/psr.hpp
    ${CF_PIOTR_SOURCES})

set(CF_MAIN_SOURCES
    src/main/image_acquisition.hpp
    src/main/image_acquisition.cpp
    src/cf_libs/dsst/dsst_tracker.hpp
    ${CF_CV_EXT_DIR}/init_box_selector.hpp
    ${CF_CV_EXT_DIR}/init_box_selector.cpp
    ${CF_CV_EXT_DIR}/tracker_run.hpp
    ${CF_CV_EXT_DIR}/tracker_run.cpp
    ${CF_TCLAP_DIR}/tclap/CmdLine.h)


# === Build static library  
add_library(${PROJECT_NAME} SHARED 
    ${CF_MAIN_SOURCES} ${CF_LIB_COMMON_SOURCES})

target_link_libraries(${PROJECT_NAME}
        ${OpenCV_LIBS}
)

target_include_directories( ${PROJECT_NAME} 
  PUBLIC
    ${OpenCV_DIR}/include
    ${CF_TCLAP_DIR}
    ${CF_CV_EXT_DIR}
    ${CF_PIOTR_DIR}
    ${CF_PIOTR_DIR}/src
    src/cf_libs/common
    src/main
    src/cf_libs/dsst
)
# ===  
